package com.ygo.feihua.view;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.EditText;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.content.res.TypedArray;
import com.ygo.feihua.R;

public class LineEditText extends EditText
{

    private Paint line;
    private int hwidth=0,lineleftpadd,linew;;
    private int linefColor=0xffaaaaaa,lineSBColor=0x66666666;
    private int linspColor=0x88666666;
    public LineEditText(Context context){
        super(context);
        init();
    }
    public LineEditText(Context context,AttributeSet As){
        this(context,As,0);
    }
    public LineEditText(Context context,AttributeSet As,int attrs){
        super(context,As,attrs);
        init();
        final TypedArray a = getContext().obtainStyledAttributes(As, R.styleable.LineEditText,attrs, 0);
        if(a==null)return;
        lineSBColor= a.getColor(R.styleable.LineEditText_lineSelectColor,lineSBColor);
        linefColor= a.getColor(R.styleable.LineEditText_lineTextColor,linefColor);
        linspColor= a.getColor(R.styleable.LineEditText_lineSplitColor,linspColor);
        lineleftpadd=(int) a.getDimension(R.styleable.LineEditText_lineSplitPadding,lineleftpadd);
        linew=(int)a.getDimension(R.styleable.LineEditText_lineSplitWidth,2);
        line.setStrokeWidth(linew);
        a.recycle();
    }
    public void setLineTextColor(int color){
        linefColor=color;
        invalidate();
    }
    public void setLineSelectColor(int color){
        lineSBColor=color;
        invalidate();
    }
    public void setLineSplitColor(int color){
        linspColor=color;
        invalidate();
    }
    public void setLineSplitWidth(int w){
        line.setStrokeWidth(w);
        invalidate();
    }
    
    private void init(){
        line=new Paint();
        line.setTextSize(getTextSize());
        line.setStyle(Paint.Style.FILL);
        lineleftpadd=(int)(getResources().getDisplayMetrics().density*5+0.5f);
        }
    @Override
    protected void onDraw(Canvas canvas)
    {
        
        int linec=getLineCount();
        int lineh=getLineHeight();
        int ll=String.valueOf(linec).length()-1;
        int y,left=(int)getTextSize()*ll+lineleftpadd;
        if(hwidth!=ll){
            setPadding(left+lineleftpadd+linew,getPaddingTop(),getPaddingRight(),getPaddingBottom());
            hwidth=ll;
        }
        
        if(getSelectionStart()>-1){
            if(getLayout()!=null){
                line.setColor(lineSBColor);
                ll=(getLayout().getLineForOffset(getSelectionStart()))*lineh+getPaddingTop();
                canvas.drawRect(left,ll,getWidth(),ll+lineh,line);
            }
        }
      
        
        super.onDraw(canvas);
           line.setColor(linspColor);
           canvas.drawLine(left,0,left,getHeight(),line);
           line.setColor(linefColor);
           for(int l=0;l<linec;l++){
            y=(l+1)*lineh+getPaddingTop()-(lineh/5);
            canvas.drawText(String.valueOf(l+1),0,y,line);
          }  
    }

    @Override
    public void setTextSize(float size)
    {   line.setTextSize(size);
        super.setTextSize(size);
    }

    
}
